
goog.provide('esn.SurveyIntro');

goog.require('esn.BaseSurvey');
goog.require('esn.dialog.ConsentDialog');
goog.require('esn.soy.surveyIntro');
goog.require('goog.ui.CustomButton');


/**
 * @constructor
 */
esn.SurveyIntro = function(appContext, opt_domHelper) {
  goog.base(this, appContext, esn.SurveyIntro.SECTION_ID, opt_domHelper);

  this.user_ = appContext.getUser();

  this.student_client_ = appContext.getService(
      esn.constants.ServiceId.STUDENT_CLIENT);
};
goog.inherits(esn.SurveyIntro, esn.BaseSurvey);

esn.SurveyIntro.SECTION_ID = 'intro';

esn.SurveyIntro.EventType = {
  CONTINUE: goog.events.getUniqueId('continue')
};

esn.SurveyIntro.prototype.continueButton_;

esn.SurveyIntro.prototype.consentDialog_;

esn.SurveyIntro.prototype.createDom = function() {
  goog.base(this, 'createDom');

  this.setElementInternal(soy.renderAsElement(esn.soy.surveyIntro.content));

  this.continueButton_ = new goog.ui.CustomButton("Begin Survey");
  this.continueButton_.render(this.getElementByClass('continueButton'));

  this.consentDialog_ = new esn.dialog.ConsentDialog();
};

esn.SurveyIntro.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this.continueButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleContinueClick_);

  // Listen for Consent Form accept.
  this.getHandler().listen(
      this.consentDialog_,
      esn.dialog.BaseDialog.EventType.ACCEPT,
      this.handleAcceptClick_);
};

esn.SurveyIntro.prototype.handleContinueClick_ = function(e) {
  if (!this.user_.hasConsented()) {
    this.consentDialog_.setVisible(true);
  } else {
    this.dispatchEvent(esn.SurveyIntro.EventType.CONTINUE);
  }
};

esn.SurveyIntro.prototype.handleAcceptClick_ = function(e) {
  this.student_client_.saveStudentConsent(true);
  // Update local user so we don't show content form on a back button click.
  this.user_.setConsented(true);
  this.consentDialog_.setVisible(false);
  this.dispatchEvent(esn.SurveyIntro.EventType.CONTINUE);
};
